﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;

namespace AvaloniaNotes.OpCommand
{
    public class Command : ICommand
    {
        /// <summary>
        /// 定义无参委托
        /// </summary>
        private Action _noParamExceute;

        /// <summary>
        /// 定义有参委托
        /// </summary>
        private Action<int> _hasParamExceute;
        /// <summary>
        /// 接受执行方法
        /// </summary>
        /// <param name="exceute"></param>
        public Command(Action exceute)
        {
            _noParamExceute = exceute;
        }

        public Command(Action<int> exceute)
        {
            _hasParamExceute = exceute;
        }

        public event EventHandler? CanExecuteChanged;

        /// <summary>
        /// 能否执行
        /// </summary>
        /// <param name="parameter"></param>
        /// <returns></returns>
        public bool CanExecute(object? parameter)
        {
            return true;
        }

        /// <summary>
        /// 执行方法
        /// </summary>
        /// <param name="parameter"></param>
        /// <exception cref="NotImplementedException"></exception>
        public void Execute(object? parameter)
        {
            if (parameter is null)
                _noParamExceute?.Invoke();
            else
                _hasParamExceute?.Invoke((int)parameter);
        }
    }
}
